import { StroageUtil } from "../../../vv/utils/StroageUtil";
import { stroageKey } from "../../constants";
import { Player } from "./Player";

//游戏图片资源加载路径
export enum IconResourcePath {
    BG = "bg/game_bg_0", //背景
    bagItem = "test/item0", //物品图标
}

//游戏相关配置数据实体类
export class Config {
    //单例
    private static _inst: Config = null;
    public static get inst() {
        if (!this._inst) {
            this._inst = new Config();
        }
        return this._inst;
    }

    public hpConsumeRate: number = 0.1; //采集体力消耗比率
    public exGrowRate: number = 0.1; //采集经验增长比率
    public cHpConsumeRate: number = 0.1; //合成体力消耗比率
    public cExGrowRate: number = 0.1; //合成经验增长比率
    public weather: number = 0;
    public season: number = 0;

    public init(hpConsumeRate: number, exGrowRate: number, cHpConsumeRate: number, cExGrowRate: number) {
        this.hpConsumeRate = hpConsumeRate;
        this.exGrowRate = exGrowRate;
        this.cHpConsumeRate = cHpConsumeRate;
        this.cExGrowRate = cExGrowRate;
    }

    //设置缓存
    public setCache(value?: Config) {
        if (!value) {
            value = this;
        }
        StroageUtil.setValue(stroageKey.config, value);
    }

    //获取缓存
    public getCache() {
        return StroageUtil.getObject(stroageKey.config);
    }

    /**
     * 获取体力消耗值
     * @param itemLv 目标物品等级
     */
    public getHpConsume(itemLv: number, isCompound: boolean = false) {
        if (isCompound) {
            return this.cHpConsumeRate * itemLv;
        }
        return this.hpConsumeRate * itemLv;
    }

    /**
     * 获取经验增长值
     * @param itemLv 目标物品等级
     */
    public getExGrow(itemLv: number, isCompound: boolean = false) {
        if (isCompound) {
            return this.cExGrowRate * itemLv;
        }
        return this.exGrowRate * itemLv;
    }

    /**
     * 玩家采集资源是否失败，返回0则成功，大于0则是失败消耗体力
     * @param itemLv 目标物品等级
     */
    public isProductionFailed(itemLv: number): number {
        let rate = 100;
        if (Player.inst.hp <= 0) {
            rate = 0;
        }
        if (rate > 0) {
            let lv = Player.inst.lv - itemLv;
            if (lv < 0) {
                rate -= 10 * Math.abs(lv);
                rate = Math.max(rate, 0);
            }
        }
        if (rate > 0) {
            rate -= Math.floor((100 - Number((Player.inst.hp / Player.inst.maxHp).toFixed(2)) * 100) * 0.5);
        }

        if (rate > 0) {
            if (rate > Math.floor(Math.random() * 99 + 1)) {
                return 0;
            } else {
                rate = 0;
            }
        }

        if (rate <= 0) {
            return Math.floor(Math.random() * 3 + 1);
        }
        return 0;
    }

    /**
     * 玩家合成是否失败，返回0则成功，大于0则是失败消耗体力
     * @param itemLv 目标物品等级
     */
    public isCompoundFailed(itemLv: number): number {
        let rate = 100;
        if (Player.inst.hp <= 0) {
            rate = 0;
        }
        if (rate > 0) {
            let lv = Player.inst.lv - itemLv;
            if (lv < 0) {
                rate -= 10 * Math.abs(lv);
                rate = Math.max(rate, 0);
            }
        }

        if (rate > 0) {
            if (rate > Math.floor(Math.random() * 99 + 1)) {
                return 0;
            } else {
                rate = 0;
            }
        }

        if (rate <= 0) {
            return Math.floor(Math.random() * 3 + 1);
        }
        return 0;
    }
}
